package com.x.linx.fenfencard;

import com.x.linx.barcode.MipcaActivityCapture;
import com.x.linx.fenfencard.map.MapDemoActivity;
import com.x.linx.fenfencard.search.SearchDemoActivity;
import com.x.linx.utils.D;

import android.R.integer;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.Toast;

public class MainActivity extends Activity {

	private static final String TAG = MainActivity.class.getSimpleName();
	public final static int SCANNIN_GREQUEST_CODE = 1;
	
	private TabHost tabHost;
	private Intent myCenterIntent;
	private Intent homeIntent;
	private Intent searchIntent;
	private RadioGroup mTabRadioGroup;

	// + init
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		D.i(TAG, "onCreate()");
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main_tab);
		// tabHost = getTabHost();
		tabHost = (TabHost) findViewById(R.id.my_tabhost);
		LocalActivityManager groupActivity = new LocalActivityManager(this,
				false);
		groupActivity.dispatchCreate(savedInstanceState);
		tabHost.setup(groupActivity);
		initIntent();
		addSpec();
		mTabRadioGroup = (RadioGroup) findViewById(R.id.tab_radiogroup);
		
		for(int i = 0; i < mTabRadioGroup.getChildCount(); ++i){
			mTabRadioGroup.getChildAt(i).setOnClickListener(mTabClickListener);
		}
//		mTabRadioGroup.setOnCheckedChangeListener(this);
	}

	private void initIntent() {
		D.i(TAG, "initIntent()");
		homeIntent = new Intent(this, HomeActivity_.class);
		searchIntent = new Intent(this, SearchDemoActivity.class);
		myCenterIntent = new Intent(this, LoginActivity_.class);
	}

	private void addSpec() {
		D.i(TAG, "addSpec()");
		tabHost.addTab(this.buildTagSpec("tab_home", -1,
				R.drawable.tab_home, homeIntent));

		tabHost.addTab(this.buildTagSpec("tab_search", -1, R.drawable.tab_search,
				searchIntent));
		
		tabHost.addTab(this.buildTagSpec("tab_my_center", -1, R.drawable.tab_personal_centre_default,
				myCenterIntent));

	}

	private TabHost.TabSpec buildTagSpec(String tagName, int tagLable,
			int icon, Intent content) {
		D.i(TAG, "buildTagSpec()");
		String lable = null;
		if (-1 != tagLable) {
			lable = getResources().getString(tagLable);
		}
		return tabHost.newTabSpec(tagName)
				.setIndicator(lable, getResources().getDrawable(icon))
				.setContent(content);
	}
	// - init

	// + tab opration
	OnClickListener mTabClickListener = new OnClickListener(){
		private final String TAG = OnClickListener.class.getSimpleName();
		@Override
		public void onClick(View arg0) {
			D.i(TAG, "onClick(), id=" + arg0.getId());
			switch (arg0.getId()) {
			case R.id.radio_button_home:
				tabHost.setCurrentTabByTag("tab_home");
				break;
			case R.id.radio_button_near:
//				tabHost.setCurrentTabByTag("tab_near");
//				startActivity(new Intent(MainActivity.this, MapDemoActivity.class));
				startActivity(new Intent(MainActivity.this, OrganizeListActivity.class));
				gotoHomeTab();
				break;
			case R.id.radio_button_barcode:
				startBarcodeScan();
				break;
			case R.id.radio_button_search:
				tabHost.setCurrentTabByTag("tab_search");
				break;
			case R.id.my_center:
				tabHost.setCurrentTabByTag("tab_my_center");
				break;
			default:
				D.i(TAG, "mTabClickListener, default ??");
				break;
			}
		}
	};
	
	void gotoHomeTab(){
		D.i(TAG, "gotoHomeTab()");
		((RadioButton)mTabRadioGroup.getChildAt(0)).setChecked(true);
        tabHost.setCurrentTabByTag("tab_home");
	}
	// - tab opration
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		D.i(TAG, "dispatchKeyEvent(): " + event);
		if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
				&& (event.getAction() == KeyEvent.ACTION_UP)) {
			// why this activity can't auto close when press back button
			D.i(TAG, "KeyEvent.KEYCODE_BACK finish()");
			finish();
		}
		return super.dispatchKeyEvent(event);
	}

	// + barcode
	void startBarcodeScan(){
		D.i(TAG, "startBarcodeScan()");
		Intent intent = new Intent();
		intent.setClass(getApplicationContext(), MipcaActivityCapture.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivityForResult(intent, SCANNIN_GREQUEST_CODE);
	}
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        D.i(TAG, "onActivityResult(), requestCode=" + requestCode
        		+ ", resultCode=" + resultCode);
        switch (requestCode) {
		case SCANNIN_GREQUEST_CODE:
			if(resultCode == RESULT_OK){
				Bundle bundle = data.getExtras();
				//显示扫描到的内容
				String result = bundle.getString("result");
				handleBarcodeScanResult(result);
				//显示
//				mImageView.setImageBitmap((Bitmap) data.getParcelableExtra("bitmap"));
			}
			break;
		}
        gotoHomeTab();
    }
	
	void handleBarcodeScanResult(String result){
		D.i(TAG, "result = " + result);
		Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
	}
	// - barcode

	// + test
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		D.i(TAG, "onDestroy");
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		D.i(TAG, "onPause");
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		D.i(TAG, "onResume");
		super.onResume();
	}
	// - test

}
